"""Geometry classes."""

__author__ = 'Josh Oosterman'

import math
import pygame


class Vector(object):
  """A 2D vector class."""

  def __init__(self, x=0, y=0):
    self.x = x
    self.y = y

  def ToAngleMagnitude(self):
    return math.degrees(math.atan2(-self.y, self.x)), self.Magnitude()

  def FromAngleMagnitude(self, angle, magnitude):
    self.x = math.cos(math.radians(angle)) * magnitude
    self.y = -math.sin(math.radians(angle)) * magnitude

  def Magnitude(self):
    return math.sqrt(float(self.x ** 2 + self.y ** 2))

  def __add__(self, other):
    return Vector(
        self.x + other.x, self.y + other.y)

  def __sub__(self, other):
    return Vector(
        self.x - other.x, self.y - other.y)

  def __mul__(self, scalar):
    return Vector(
        self.x * scalar, self.y * scalar)

  def __div__(self, scalar):
    return self * (1.0 / scalar)

  def __neg__(self):
    return -1.0 * self

  def Normalize(self):
    length = self.Magnitude()
    if length > 0:
      self.x /= length
      self.y /= length
    return self


class Rect(object):
  """A 2D rectangle class."""

  def __init__(self, x=0, y=0, width=0, height=0):
    self.x = x
    self.y = y
    self.width = width
    self.height = height

  def __contains__(self, vector):
    return (vector.x > self.x and
            vector.x < self.x + self.width and
            vector.y > self.y and
            vector.y < self.y + self.height)

  def Size(self):
    return Vector(self.width, self.height)

  def ToPygameRect(self):
    return pygame.Rect(self.x, self.y, self.width, self.height)


def SetLocation(rect, vector):
  """Set the location of a rect to a vector."""
  rect.left = vector.x
  rect.top = vector.y